//
//  XLAllApplyView.m
//  industry
//
//  Created by xl on 2020/11/27.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLAllApplyView.h"
#import "XLAllApplyViewProtocol.h"
#import "XLAllApplySearchBarView.h"
#import "XLAllApplyAdapter.h"
#import "XLAllApplyVC.h"
#import "XLAppletModel.h"
#import "XLAllApplyPresenterProtocol.h"

@interface XLAllApplyView ()<XLAllApplyViewProtocol, XLCollectionAdapterDelegate>

@property (nonatomic, strong) AUToast *toastLoad;
@property (nonatomic, strong) XLAllApplySearchBarView *searchView;
@property (nonatomic, strong) XLCollectionView *collectionView;
@property (nonatomic, strong) XLAllApplyAdapter *adapter;
@property (nonatomic, strong) NSArray *commonArr;
@property (nonatomic, strong) NSArray *allArr;

@end
@implementation XLAllApplyView

- (void)dealloc {
    XLLog(@"XLAllApplyView dealloc");
}

#pragma mark - XLAllApplyViewProtocol
- (void)buildView {
    [self addSubview:self.searchView];
    [self addSubview:self.collectionView];
    [_searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self).offset(10);
        make.height.mas_equalTo(50);
    }];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.top.equalTo(self).offset(70);
    }];
    
    XLAllApplyVC *vc = (XLAllApplyVC *)self.baseController;
    for (XLAppletModel *mode in vc.commonArr) {
        mode.isCommon = YES;
    }
    _adapter.commonArr = vc.commonArr;
    _adapter.allArr = vc.allArr;
    self.commonArr = vc.commonArr;
    self.allArr = vc.allArr;
    [_collectionView reloadData];
}

- (void)refreshCurrentViewLayoutWithIsEdit:(BOOL)state{
    
    if (state) {
        [_collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
        }];
    }else{
        
    }
    
    _searchView.hidden = state;
    self.adapter.isEdit = state;
    [_collectionView reloadData];
}

- (void)beginSearchBar {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"SELF CONTAINS '%@'", _searchView.getCurrentSearchText]];
    NSMutableArray *temComArr = [NSMutableArray array];
    NSMutableArray *temAllArr = [NSMutableArray array];
    for (XLAppletModel *model in self.commonArr) {
        if ([predicate evaluateWithObject:model.appletName]) {
            [temComArr addObject:model];
        }
    }
    for (XLAppletModel *model in self.allArr) {
        if ([predicate evaluateWithObject:model.appletName]) {
            [temAllArr addObject:model];
        }
    }
    if (ISEmptyString(_searchView.getCurrentSearchText)) {
        _adapter.commonArr = self.commonArr;
        _adapter.allArr = self.allArr;
    } else {
        _adapter.commonArr = temComArr;
        _adapter.allArr = temAllArr;
    }
    [_collectionView reloadData];
}

- (void)showLoadingAnimalWithTitle:(NSString *)title {
    self.toastLoad = [XLToast showLoadingTitle:title superView:self];
}

- (void)hideLoadingAnimal {
    if (self.toastLoad) {
        [self.toastLoad dismissToast];
    }
}

- (void)refreshAddApplet:(XLAppletModel *)model {
    NSDictionary *temDic = model.mj_keyValues;
    
    XLAppletModel *newModel = [XLAppletModel mj_objectWithKeyValues:temDic];
    newModel.isCommon = YES;
    NSMutableArray *temArr = [NSMutableArray array];
    [temArr addObjectsFromArray:self.commonArr];
    [temArr addObject:newModel];
    self.commonArr = temArr;
    _adapter.commonArr = temArr;
    [_collectionView reloadData];
    
    XLAllApplyVC *vc = (XLAllApplyVC *)self.baseController;
    vc.isChange = YES;
}

- (void)refreshDeleteApplet:(XLAppletModel *)model {
    NSMutableArray *temArr = [NSMutableArray array];
    [temArr addObjectsFromArray:self.commonArr];
    [temArr removeObject:model];
    self.commonArr = temArr;
    _adapter.commonArr = temArr;
    [_collectionView reloadData];
    
    XLAllApplyVC *vc = (XLAllApplyVC *)self.baseController;
    vc.isChange = YES;

}

- (XLAllApplyAdapter *)getAdapter{
    return self.adapter;
}

#pragma mark - XLCollectionAdapterDelegate
- (void)didSelectCellData:(id)cellData {
    XLAppletModel *model = (XLAppletModel *)cellData;
    if (_adapter.isEdit && !model.isCommon) {
        if (self.commonArr.count > 6) {
            [XLToast showComonMessage:@"常用小程序不能超过7个"];
            return;
        }
        BOOL isContain = NO;
        for (XLAppletModel *temModel in self.commonArr) {
            if ([temModel.appletId isEqualToNumber:model.appletId]) {//表示常用小程序已经包含该小程序，不能再添加
                isContain = YES;
                break;
            }
        }
        if (isContain) {
            [XLToast showComonMessage:@"常用小程序已经包含该小程序了"];
            return;
        }
    }
    XLSend(self.baseController.presenter, XLAllApplyPresenterProtocol, handleCellData:cellData isEdit:_adapter.isEdit);
}

#pragma mark - Setter/getter
- (XLAllApplySearchBarView *)searchView {
    if (!_searchView) {
        _searchView = [[XLAllApplySearchBarView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    }
    return _searchView;
}

- (XLCollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
        flow.scrollDirection = UICollectionViewScrollDirectionVertical;
        _collectionView = [[XLCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flow];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.bounces = NO;
        _collectionView.delegate = self.adpter;
        _collectionView.dataSource = self.adpter;
        
        //注册cell
        [_collectionView registerClass:[XLAllApplyCell class] forCellWithReuseIdentifier:@"XLAllApplyCell"];
        [_collectionView registerClass:[XLAllApplyHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"XLAllApplyHeaderView"];
    }
    return _collectionView;
}

- (XLAllApplyAdapter *)adpter {
    if (!_adapter) {
        _adapter = [[XLAllApplyAdapter alloc] init];
        _adapter.adapterDelegate = self;
    }
    return _adapter;
}

@end
